import axios from 'axios';
import qs from 'qs'

// 本地运行地址
//axios.defaults.baseURL = 'http://192.168.1.150:8090/ccmc';
//axios.defaults.baseURL = 'http://localhost:8080/ccmc';
//axios.defaults.baseURL = 'http://192.168.1.145:8021';
//axios.defaults.baseURL = 'http://192.168.6.87:9090'
// 上线（打包）地址
axios.defaults.baseURL = 'http://dxccmc.com'

axios.defaults.timeout = 5000;
// 让每个请求携带token--['token']为自定义key
//axios.defaults.headers.common['Authorization'] = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIzZTkwMTg4Ny02ZjlmLTRiYjgtYTA3ZC0wMjliZTUyNzc4ZWMiLCJzdWIiOiJERDY1Q0ZDMTNCRkZFRUMxQ0VCNEY1MTAzOEUyMzA1RSIsImlhdCI6MTUyOTczODI4NiwiZXhwIjoxNTMyMzMwMjg2fQ.5ukUWdyKGchEEMTfWefEnbrfY1N5EOYlG9t35p3TATjoresfLzZVW1DSCJFr9vO3e-Z4WkYnN2SHhm0rcrsN1Q'
// 跨域请求时是否需要使用凭证 
axios.defaults.withCredentials = true;




// 添加请求拦截器
axios.interceptors.request.use(config => {
	var a = localStorage.getItem('token');
	const token = 'Bearer ' + a;
//	const token = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIwYmEwZGU2MS00MTlhLTQwYjMtODBlMS1hODc3YTFjODM4NWEiLCJzdWIiOiIyQzY1OTlFRjRCNDhGNkY5NzQxQTZENTdCN0M2RkYzRCIsImlhdCI6MTU0NTE5OTg4MiwiZXhwIjoxNTQ3NzkxODgyfQ.7LCVcr5GUTXPm45VvoS6IyKamlAn_mE1nItX_SRTfixHaLBfH335TghPyainvmQruRBBRnrzEb6-Xg9Cq6AfEw'; 

			
    if (token) {
// 这里将token设置到headers中，header的key是Authorization，这个key值根据你的需要进行修改即可
        config.headers.Authorization = token;
        axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
    } else{
    	config.headers.Authorization = token;
    	axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
    }
//  console.log(token)
   
	
//  if (Array.isArray(config.data)) {
//      axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
//
//      
////		axios.defaults.headers.common['Authorization'] = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiJiMjY0MWZiNi03ZDI5LTQzMzgtOWU0NS04NWEwZTRkYWVjMjAiLCJzdWIiOiIyMDE5Rjg2N0ZBQjE0RENDMUQ0N0NEMURBNkE1QzA0QyIsImlhdCI6MTUyOTgzMTk1NCwiZXhwIjoxNTMyNDIzOTU0fQ.3ld10icBzu-cNUgtxDffADEU5wLlxVGInyv3SvRGcPB4ITPWa3AaTZkpald2GU9S7MBSUOXLR4eOqfnFW3b_AA'
//  } else {
////		axios.defaults.headers.common['Authorization'] = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiJiMjY0MWZiNi03ZDI5LTQzMzgtOWU0NS04NWEwZTRkYWVjMjAiLCJzdWIiOiIyMDE5Rjg2N0ZBQjE0RENDMUQ0N0NEMURBNkE1QzA0QyIsImlhdCI6MTUyOTgzMTk1NCwiZXhwIjoxNTMyNDIzOTU0fQ.3ld10icBzu-cNUgtxDffADEU5wLlxVGInyv3SvRGcPB4ITPWa3AaTZkpald2GU9S7MBSUOXLR4eOqfnFW3b_AA'
//  
////      axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
//      axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
//      if (config.method === 'post') {
//          config.data = qs.stringify(config.data);
//      }
//  }
//console.log(config)
    return config;
}, error => {
    console.log(error);
    Promise.reject(error);
});

// 添加响应拦截器
//axios.interceptors.response.use(
//  response => {
//      if (response.data.status === 2005) {
//          localStorage.clear();
//          location.href = "./";
//          alert("登录已超时，请重新登录！")
//      }
//      return response
//  }, error => {
//      console.log('err' + error);
//      return Promise.reject(error);
//  });

export default axios;